package special;

import java.util.ArrayList;
import java.util.HashSet;
import util.Pair;

/**
 * @author Grega
 */
public class TreeNode
{
  public ArrayList<Edge<TreeNode>> children;
  public Edge<TreeNode> parent;
  public Flower flower;
  public Integer depth = 0;

  public TreeNode(Flower value)
  {
    parent = null;
    children = new ArrayList<Edge<TreeNode>>();
    this.flower = value;
  }

  public TreeNode addChild(Flower value, Pair label)
  {
    children.add(new Edge<TreeNode>(new TreeNode(value), label));
    children.get(children.size() - 1).connectedTo.depth = depth + 1;
    children.get(children.size() - 1).connectedTo.parent =
            new Edge<TreeNode>(this, label);
    return children.get(children.size() - 1).connectedTo;
  }

  public TreeNode addChild(Edge value)
  {
    children.add(value);
    return children.get(children.size() - 1).connectedTo;
  }

  @Override
  public boolean equals(Object obj)
  {
    TreeNode t;
    if (obj instanceof TreeNode) {
      t = (TreeNode)obj;
      return flower.equals(t.flower) && children.equals(t.children) && parent.
              equals(t.parent);
    }
    return false;
  }

  @Override
  public int hashCode()
  {
    int hash = 5;
    hash = 67 * hash + (this.children != null ? this.children.hashCode() : 0);
    hash = 67 * hash + (this.parent != null ? this.parent.hashCode() : 0);
    hash = 67 * hash + (this.flower != null ? this.flower.hashCode() : 0);
    return hash;
  }

  @Override
  public String toString()
  {
    return flower.toString() + ", [ " + children.toString() + " ]";
  }

  // get all vertexes in T on depth%2==(evenDepth ? 0 : 1) is true
  public HashSet<Integer> getVInTree(boolean evenDepth)
  {
    HashSet<Integer> temp = new HashSet<Integer>();

    if (depth % 2 == (evenDepth ? 0 : 1)) {
      temp.addAll(flower.vertexSet);
    }

    if (!children.isEmpty()) {
      for (int i = 0; i < children.size(); i++) {
        temp.addAll(children.get(i).connectedTo.getVInTree(evenDepth));
      }
    }
    return temp;
  }

}
